package com.lelefly.base.serviceImp;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.lelefly.base.daoimp.BaseDao;
import com.lelefly.base.model.Result;
import com.lelefly.base.po.VersionUpdate;
import com.lelefly.constant.Constant;
import com.lelefly.handleexecption.ServiceExecption;

@Service
public class VersionService {

	@Resource
	public BaseDao baseDao;
	
	@SuppressWarnings({ "null", "rawtypes" })
	public Result getVsesionService() {
		Result<VersionUpdate> result = new Result<>();
		try {
			String hql = "from VersionUpdate as model where id = 1";
			List<VersionUpdate> resultsList= baseDao.queryObjetReList(hql);
			if (resultsList.size()!=1) {
				throw new ServiceExecption("版本错误");
			}
			result.setState(Constant.HTTP_STATUS_OK);
			result.setResultList(resultsList);
		}catch(Exception e){
			result.setState(Constant.HTTP_STATUS_FAIL);
			result.setMes(Constant.HTTP_STATUS_DEFERROR);
		}catch (ServiceExecption e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			result.setState(Constant.HTTP_STATUS_FAIL);
			result.setMes(e.getMessage());
		}
		return result;
	}
	
	@SuppressWarnings({ "null", "rawtypes" })
	public Result saveVsesionService(String versioncode) {
		Result<VersionUpdate> result = new Result<>();
		try {
			if (StringUtils.isEmpty(versioncode)) {
				throw new ServiceExecption("版本号不正确");
			}
			VersionUpdate versionUpdate = new VersionUpdate(versioncode);
			versionUpdate.setId(1);
		     baseDao.UpdateObject(versionUpdate);
			result.setState(Constant.HTTP_STATUS_OK);
		}catch(Exception e){
			result.setState(Constant.HTTP_STATUS_FAIL);
			result.setMes(Constant.HTTP_STATUS_DEFERROR);
		}catch (ServiceExecption e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			result.setState(Constant.HTTP_STATUS_FAIL);
			result.setMes(e.getMessage());
		}
		return result;
	}
}
